home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / CW GUSI 1.6.4 / include / GUSIFile_P.h < prev    next >
C/C++ Source or Header  |  1995-10-15  |  6KB  |  226 lines

  1. /*********************************************************************
  2. Project    :    GUSI                -    Grand Unified Socket Interface
  3. File        :    GUSIFile_P.h    -    Common Definitions for File Sockets
  4. Author    :    Matthias Neeracher
  5. Language    :    MPW C/C++
  6.  
  7. $Log: GUSIFile_P.h,v $
  8. Revision 1.2  1994/12/31  02:04:05  neeri
  9. Reorganize file name dispatching.
  10.  
  11. Revision 1.1  1994/08/10  00:40:37  neeri
  12. Initial revision
  13.  
  14. *********************************************************************/
  15.  
  16. #include "GUSI_P.h"
  17.  
  18. extern int File_error(OSErr err);
  19. extern Boolean IsDevice(const char * fn);
  20.  
  21.  
  22. #ifndef PRAGMA_ALIGN_SUPPORTED
  23. #error Apple had some fun with the conditional macros again
  24. #endif
  25.  
  26. #if PRAGMA_ALIGN_SUPPORTED
  27. #pragma options align=mac68k
  28. #endif
  29.  
  30. class FileSocket : public Socket {
  31.     friend class FileSocketDomain;
  32. protected:
  33.     short            fRefNum;
  34.     Boolean        append;
  35.                     FileSocket(short fRefNum)    :    fRefNum(fRefNum)    {}
  36.     
  37.     void SetFRefNum(short fRefNum) { this->fRefNum = fRefNum; }
  38.     void PrepareWrite()               { if (append) lseek(0, SEEK_END); }
  39. public:
  40.     virtual int    fcntl(unsigned int cmd, int arg);
  41.     virtual int    ioctl(unsigned int request, void *argp);
  42.     virtual int    fstat(struct stat * buf);
  43.     virtual int select(Boolean * canRead, Boolean * canWrite, Boolean * exception);
  44.     virtual         ~FileSocket();
  45. };
  46.  
  47. class MPWFileSocket : public FileSocket {
  48.     friend class FileSocketDomain;
  49.     friend class MPWDomain;
  50. protected:
  51.     int            fd;
  52.                     MPWFileSocket(int fd);
  53.                     
  54.     static MPWFileSocket    *    open(const char * name, int flags);
  55.     static MPWFileSocket    *    stdopen(int fd);
  56. public:
  57.     virtual int    read(void * buffer, int buflen);
  58.     virtual int write(void * buffer, int buflen);
  59.     virtual int    fcntl(unsigned int cmd, int arg);
  60.     virtual int    ioctl(unsigned int request, void *argp);
  61.     virtual long lseek(long offset, int whence);
  62.     virtual int ftruncate(long offset);
  63.     virtual int    fstat(struct stat * buf);
  64.     virtual int    isatty();
  65.     virtual         ~MPWFileSocket();
  66. };
  67.  
  68. class MacFileSocket : public FileSocket {
  69.     friend class FileSocketDomain;
  70. protected:
  71.     MacFileSocket(short fRef) : FileSocket(fRef) {}
  72.                     
  73.     static MacFileSocket *    open(const TFileSpec & spec, int flags);
  74. public:
  75.     virtual int    read(void * buffer, int buflen);
  76.     virtual int write(void * buffer, int buflen);
  77.     virtual int    ioctl(unsigned int request, void *argp);
  78.     virtual long lseek(long offset, int whence);
  79.     virtual int ftruncate(long offset);
  80.     virtual         ~MacFileSocket();
  81. };
  82.  
  83. #if PRAGMA_ALIGN_SUPPORTED
  84. #pragma options align=reset
  85. #endif
  86.  
  87. // FileSocketDomain is the spiritual leader for all domains that deal
  88. // with file names. Descendants claim their interest in device names 
  89. // and normal names on creation. 
  90. //    When their Yours() routine is called, they have to give a definite
  91. // answer on whether they'll take responsibility for a given file/request
  92. // combination. They have the right to cache information in static areas,
  93. // as no second call to Yours() will be made before the actual routine is
  94. // called. 
  95.  
  96. class GUSIFileRef;
  97.  
  98. class FileSocketDomain : public SocketDomain {
  99.     friend SocketTable::SocketTable();
  100.     friend int SocketTable::Install(Socket * sock, int start);
  101.  
  102.     FileSocketDomain *    nextDeviceDomain;
  103.     FileSocketDomain *     nextFileDomain;
  104. protected:
  105.     static FileSocketDomain *    firstDeviceDomain;
  106.     static FileSocketDomain *     lastDeviceDomain;
  107.     static FileSocketDomain *    firstFileDomain;
  108.     static FileSocketDomain *     lastFileDomain;
  109.  
  110.     FileSocketDomain(
  111.         int         domain, 
  112.         Boolean     doesDevices, 
  113.         Boolean     doesFiles);
  114. public:
  115.     FileSocketDomain();
  116.     virtual ~FileSocketDomain();
  117.     
  118.     enum Request {
  119.         willOpen,
  120.         willRemove,
  121.         willRename,
  122.         willGetFileInfo,
  123.         willSetFileInfo,
  124.         willFAccess,
  125.         willStat,
  126.         willChmod,
  127.         willUTime,
  128.         willAccess
  129.     };
  130.     
  131.     static FileSocketDomain * FindDomain(const GUSIFileRef & ref, FileSocketDomain::Request request);
  132.         
  133.     virtual Boolean Yours(const GUSIFileRef & ref, FileSocketDomain::Request request);
  134.     
  135.     virtual Socket * open(const GUSIFileRef & ref, int oflag);
  136.     virtual int remove(const GUSIFileRef & ref);
  137.     virtual int rename(const GUSIFileRef & ref, const char *newname);
  138.     virtual void 
  139.         fgetfileinfo(
  140.             const GUSIFileRef & ref, 
  141.             unsigned long * creator, unsigned long * type);
  142.     virtual void 
  143.         fsetfileinfo(
  144.             const GUSIFileRef & ref, 
  145.             unsigned long creator, unsigned long type);
  146.     virtual int 
  147.         faccess(const GUSIFileRef & ref, unsigned int cmd, long* arg);
  148.     virtual int stat(const GUSIFileRef & ref, struct stat * buf);
  149.     virtual int chmod(const GUSIFileRef & ref, mode_t mode);
  150.     virtual int utime(const GUSIFileRef & ref, const struct utimbuf * times);
  151.     virtual int access(const GUSIFileRef & ref, int mode);
  152.  
  153.     virtual int choose(
  154.                         int         type, 
  155.                         char *     prompt, 
  156.                         void *     constraint,        
  157.                         int         flags,
  158.                          void *     name, 
  159.                         int *     namelen);
  160. };
  161.  
  162. // A GUSIFileRef is an immutable structure, consisting of a pointer to 
  163. // a path name, a pointer to a TFileSpec (if the path is a plain file),
  164. // and a pointer to the FileSocketDomain which will handle it. 
  165.  
  166. #if PRAGMA_ALIGN_SUPPORTED
  167. #pragma options align=mac68k
  168. #endif
  169.  
  170. class GUSIFileRef {
  171.     OSErr                        error;
  172.     Boolean                    hasInfo;
  173.     TFileSpec                file;
  174.     CInfoPBRec                info;
  175.     FileSocketDomain *     domain;
  176. public:
  177.     const char *            name;
  178.     TFileSpec *                spec;
  179.     
  180.     GUSIFileRef(const char * name, FileSocketDomain::Request request, Boolean useAlias = false);
  181.     GUSIFileRef(short fRefNum, FileSocketDomain::Request request);
  182.     
  183.     Boolean                     IsDevice()     const {     return !spec;     }
  184.     OSErr                        Error()        const {     return error;     }
  185.     const CInfoPBRec *    Info()        const;    // conceptually const
  186.     FileSocketDomain *    Domain()        const    {    return domain;    }
  187. };
  188.  
  189. #if PRAGMA_ALIGN_SUPPORTED
  190. #pragma options align=reset
  191. #endif
  192.  
  193. extern FileSocketDomain *    FileSockets;
  194.  
  195. class MPWDomain : public SocketDomain {
  196. public:
  197.     MPWDomain();
  198.     
  199.     static MPWFileSocket * (*open)(const char * name, int flags);
  200.     static MPWFileSocket * (*stdopen)(int fd);
  201. };
  202.  
  203. extern MPWDomain MPWSockets;
  204.  
  205. // 
  206. // Null sockets always return EOF on reading and act as bottomless sinks on writing
  207. //
  208.  
  209. class NullSocketDomain : public FileSocketDomain {
  210.     Socket *    singleton;
  211. public:
  212.     NullSocketDomain() : FileSocketDomain(AF_UNSPEC, true, false), singleton(nil) {}
  213.         
  214.     virtual Boolean Yours(const GUSIFileRef & ref, FileSocketDomain::Request request);
  215.     
  216.     virtual Socket * open(const GUSIFileRef & ref, int oflag);
  217. };
  218.  
  219. __BEGIN_DECLS
  220. int fwalk(int (*func)(FILE * stream));
  221.  
  222. typedef Boolean(*GUSIExecFn)(const GUSIFileRef & ref);
  223. extern GUSIExecFn GUSIExec;
  224. Boolean GUSIDefaultExec(const GUSIFileRef & ref);
  225. __END_DECLS
  226.